﻿$.fn.zoomImg=function(params){
    var options={
        maxWidth:null,
        maxHeight:null,
        loop:100
    }
    if(params!=null){
        $.extend(options,params);
    }
    function execute(dom,img,loop){
        if(img.width<=0 || img.height<=0){
            if(loop>0) setTimeout(function(){execute(dom,img,--loop)},500);
            return;
        }
        
        var hasWidth=true;
        if(options.maxWidth==null){
            options.maxWidth=img.width;
            hasWidth=false;
        }
        var hasHeight=true;
        if(options.maxHeight==null){
            options.maxHeight=img.height;
            hasHeight=false;
        }
        
        var xScale=1;
        var yScale=1;
        var scale=1;
        xScale=options.maxWidth/img.width;
        yScale=options.maxHeight/img.height;   
        if(xScale>1 && yScale>1){
                scale=1;
        }
        else{
            scale=Math.min(xScale,yScale);
        }
        
        var margin_X=0;
        if(hasWidth) margin_X=(options.maxWidth-img.width*scale)/2;
        var margin_Y=0;
        if(hasHeight) margin_Y=(options.maxHeight-img.height*scale)/2;
        
        var original={};
        original.width=$(dom).css("width");
        original.height=$(dom).css("height");
        original.marginTop=$(dom).css("marginTop");
        original.marginRight=$(dom).css("marginRight");
        original.marginBottom=$(dom).css("marginBottom");
        original.marginLeft=$(dom).css("marginLeft");
        $(dom).data("original",original);
        $(dom).css({width:img.width*scale,height:img.height*scale})
              .css("marginTop",parseInt(original.marginTop) + margin_Y)
              .css("marginRight",parseInt(original.marginRight) + margin_X)
              .css("marginBottom",parseInt(original.marginBottom) + margin_Y)
              .css("marginLeft",parseInt(original.marginLeft) + margin_X);
        var zoom="";
        if(typeof($(this).attr("zoom"))!="undefined"){
            zoom=$(this).attr("zoom") + " ";
        }
        zoom+=options.maxWidth + "_" + options.maxHeight;
        $(dom).attr("zoom",zoom);
        
    }
    this.each(function(){        
        if(!$(this).is("img")) return;
        if(typeof($(this).attr("zoom"))!="undefined" && $(this).attr("zoom").indexOf(options.maxWidth + "_" + options.maxHeight)!=-1) return;
        var img=new Image();
        img.src=this.src;
        execute(this,img,options.loop);        
    }); 
    return this;   
}
$.fn.unZoomImg=function(){
    return this.each(function(){        
        if(!$(this).is("img")) return;
        if(typeof($(this).attr("zoom"))!="undefined"){
            var original=$(this).data("original");
            $(this).css({width:original.width,height:original.height,marginTop:original.marginTop,marginRight:original.marginRight,marginBottom:original.marginBottom,marginLeft:original.marginLeft})
            $(this).removeAttr("zoom");
        }    
    })
}